<template>
  <div class="chooseIcons">

    <el-popover placement="top-start" :append-to-body="false" width="30vw" trigger="click">
      <div slot="reference" class="iconDiv flex">
        <i class="icon-icon flex-box" :class="userChooseIcon"
         v-if="userChooseIcon"></i>
<!--        <div class="iconName">{{ chooseIcons}}</div>-->
        <div class="iconName flex1 fs14">{{ userChooseIcon ? userChooseIcon : '请选择图标'}}</div>
      </div>
      <div class="iconList">
        <i
          v-for="item in iconList"
          :key="item"
          :class="[item, 'icon']"
          @click="setIcon(item)"
          style="font-size: 20px"
        ></i>
      </div>
    </el-popover>
  </div>
</template>

<script>
import {elementIcons} from '@/utils/icon'
export default {
  name: "IconChose",
  props:{
    chooseIcons:{
      type:String,
      default:''
    },
  },
  data() {
    return {
      userChooseIcon:"",
      iconList:[]
    }
  },
  watch:{
    chooseIcons(newVal,oldVal){
      if(newVal == ''){
        this.setIcon('')
      }
    },
  },
  created() {
    //created里使用
    this.iconList=elementIcons;
  },
  methods:{
    //给icon绑定的点击事件
    setIcon(icon){
      console.log(icon)
      this.userChooseIcon=icon;//将i的样式设为选中的样式el-icon-xxx
      this.$emit('setIcon',icon)
    },
  }
}
</script>

<style lang="less" scoped>
.chooseIcons {
  width: 100%;

  .iconList {
    width: 30vw;
    height: 300px;
    overflow-y: scroll;
    overflow-x: hidden;
    display: flex;
    justify-content: space-around;
    flex-wrap: wrap;
  }
  .icon {
    display: inline-block;
    width: 60px;
    height: 45px;
    color: #000000;
    font-size: 20px;
    border: 1px solid #e6e6e6;
    border-radius: 4px;
    cursor: pointer;
    text-align: center;
    line-height: 45px;
    margin: 5px;

  }
  .icon:hover {
    color: red;
    border-color: red;
  }
  .iconDiv {
    width: 100%;
    height: 50%;

    display: flex;
    flex-direction: row;
    align-items: center;
  }

  .icon-icon{
    width: 45px;
    height: 45px;
    font-size: 25px;
  }

  .iconName {
    //width: 50%;
    height: 32px;
    line-height: 32px;
    text-align: center;
    border: 1px solid #eee;
  }

}
</style>
